package com.example.mapper;

import com.example.model.NetGDId;
import com.example.model.NotifyInfo;
import com.example.model.UserOrder;
import com.example.model.UserRequest;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.Base64;
import java.util.List;

/**
 * Created by ykbboom on 2017/3/5.
 */
@Mapper
@Component
public interface RequestMapper {
    @Insert(value = "insert into request(gd_id_start,gd_id_stop,description,place,destination,time,picurl,user_id) values(#{gdIdStart},#{gdIdStop},#{description},#{place},#{destination},#{time},#{picurl},#{userId})")
    Integer publishRequest(UserRequest userRequest);

    @Select(value = { "select * from request where gd_id_start = #{gdIdStart}" })
    @ResultMap("com.example.mapper.RequestMapper.RequestResult")
    UserRequest getUserRequestDetailByGdId(int gdIdStart);
   
    @Select(value = { "select * from request where request_id = #{requestId}" })
    @ResultMap("com.example.mapper.RequestMapper.RequestResult")
    UserRequest getUserRequestDetailByRequestId(int requestId);
    
    @Select(value = { "select * from request where user_id = #{userId} order by request_id DESC" })
    @ResultMap("com.example.mapper.RequestMapper.RequestResult")
    List<UserRequest> getUserAllRequest(int userId);

    @Select(value = {"select COUNT(user_id) from request where user_id = #{userId} "})
    Integer getUserRequestCount(int userId);


    @Delete(value = {"DELETE FROM request WHERE request_id = #{requestId}"})
    Integer deleteUserRequest(int requestId);

    @Delete(value = {"DELETE FROM user_order WHERE request_id = #{requestId}"})
    Integer deleteUserOrder(int requestId);

    @Select(value = {"select gd_id_start,gd_id_stop from request where request_id = #{requestId}"})
    @Results({
            @Result(column = "gd_id_start", property = "startId"),
            @Result(column = "gd_id_stop", property = "stopId")
    })
    NetGDId getGDId(int requestId);

    @Insert(value = "insert into user_order(request_id,driver_id) values(#{requestId},#{driverId})")
    Integer acceptRequest(UserOrder userOrder);

    @Select(value = {"select COUNT(driver_id) from user_order where driver_id = #{driverId} "})
    Integer getUserOrderCount(int driverId);

    @Update(value = {"update request set accept = 1 where request_id = #{requestId}"})
    Integer updateUserAccept(int requestId);

    @Select(value = {"select request_id from request where gd_id_start = #{gdIdStart} "})
    Integer getRequestIdByGdIdStart(int gdIdStart);

    @Select(value = {"select driver_id from user_order where request_id = #{requestId} "})
    Integer getDriverIdByRequestId(int requestId);

    @Select(value = {"select accept from request where request_id = #{requestId} "})
    Integer checkRequestAccepted(int requestId);

    @Select(value = {"select request_id from user_order where driver_id = #{driverId} "})
    List<Integer> getDriverAllOrder(int driverId);

    @Update(value = {"update request set complete = 1 where request_id = #{requestId}"})
    Integer updateUserComplete(int requestId);

    @Update(value = {"update user_order set complete = 1 where request_id = #{requestId}"})
    Integer updateDriverComplete(int requestId);

    @Select(value = {"select complete from user_order where request_id = #{requestId} "})
    Integer getCompleteInOrderByRequestId(int requestId);

    @Select(value = {"select notify_accept,request_id,accept from request where user_id = #{userId} "})
    @Results({
            @Result(column = "notify_accept", property = "notifyFlag"),
            @Result(column = "request_id", property = "requestId"),
            @Result(column = "accept", property = "accept")
    })
    List<NotifyInfo> getNotifyFlagByUserId(int userId);

    @Update(value = {"update request set notify_accept = 1 where request_id = #{requestId}"})
    Integer updateNotifyFlag(int requestId);
}
